//
//  CustomProgress.m
//  qq刷新动画
//
//  Created by MyMac on 15/7/28.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "CustomProgress.h"
@interface CustomProgress()

@property(nonatomic,assign)CGPoint point;
@end
@implementation CustomProgress

-(instancetype)initWithFrame:(CGRect)frame{
    if([super initWithFrame:frame]){
        self.value=0;
        self.backColor=[UIColor whiteColor];
        self.progressColor=[UIColor lightGrayColor];
        self.displayColor=[UIColor blueColor];
        self.cacheValue=0;
        self.btn=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, 20, 20)];
        self.btn.center=CGPointMake(5,self.frame.size.height/2.0);
        self.btn.userInteractionEnabled=YES;
        self.userInteractionEnabled=YES;
        UIPanGestureRecognizer*pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(progressClick:)];
        [self.btn addGestureRecognizer:pan];

        [self addSubview:self.btn];
        self.btn.backgroundColor=[UIColor whiteColor];
     
        self.btn.layer.cornerRadius=self.btn.bounds.size.width/2.0;
        self.btn.layer.masksToBounds=YES;
    
    
    }

    return self;


}
-(void)progressClick:(UIPanGestureRecognizer*)btn{
   // btn.center=self.point;
    CGPoint point=[btn locationInView:self];
    if(point.x>=10&&point.x<=self.frame.size.width-5){
    self.btn.center=CGPointMake(point.x, self.frame.size.height/2.0);
    self.value=point.x/self.frame.size.width;
    [self setNeedsDisplay];
    }




}
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    UITouch *touch=[touches anyObject];
//    self.point=[touch locationInView:self];
//    self.btn.center=self.point;
//    
//    NSLog(@"%@",NSStringFromCGPoint(self.point));
//
//
//}
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    UITouch *touch=[touches anyObject];
//    self.point=[touch locationInView:self];
//    self.btn.center=self.point;
//
//    NSLog(@"%@",NSStringFromCGPoint(self.point));
//
//
//
//}
-(void)drawRect:(CGRect)rect
{
    
    CGContextRef ctf=UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctf, 10, rect.size.height/2.0);
    CGContextSaveGState(ctf);
    CGContextSetStrokeColorWithColor(ctf, self.backColor.CGColor);
    CGContextSetLineWidth(ctf, 2);
    CGContextAddLineToPoint(ctf, rect.size.width-10,rect.size.height/2.0);
    CGContextStrokePath(ctf);
    CGContextRestoreGState(ctf);
    CGContextSaveGState(ctf);
    CGContextMoveToPoint(ctf, 10, rect.size.height/2.0);
    CGContextAddLineToPoint(ctf, (rect.size.width-10)*self.cacheValue,rect.size.height/2.0);
    CGContextSetLineWidth(ctf, 2);
    CGContextSetStrokeColorWithColor(ctf, self.progressColor.CGColor);
    CGContextStrokePath(ctf);
    CGContextRestoreGState(ctf);

      CGContextMoveToPoint(ctf, 10, rect.size.height/2.0);
     CGContextAddLineToPoint(ctf, (rect.size.width-10)*self.value,rect.size.height/2.0);
    CGContextSetLineWidth(ctf, 2);
    CGContextSetStrokeColorWithColor(ctf, self.displayColor.CGColor);
    CGContextStrokePath(ctf);
   

    



}
-(void)setProgressColor:(UIColor *)progressColor
{
    _progressColor=progressColor;
   // [self setNeedsDisplay];
}
-(void)setValue:(CGFloat)value
{
    _value=value;
    self.btn.center=CGPointMake((self.frame.size.width-10)*_value,self.frame.size.height/2.0);
    [self setNeedsDisplay];

}
-(void)setCacheValue:(CGFloat)cacheValue
{
    _cacheValue=cacheValue;
    


}
@end
